<?php
// 加载通用文件
require_once __DIR__ . '/../common/autoload.php';

// 检查是否已安装
if (!file_exists(__DIR__ . '/../.installed') && basename($_SERVER['PHP_SELF']) !== 'install.php') {
    header('Location: /install.php');
    exit;
}

// 检查AJAX请求
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
    header('HTTP/1.1 403 Forbidden');
    echo json_encode(['success' => false, 'message' => '禁止直接访问']);
    exit;
}

// 检查登录状态
check_login();

// 验证用户是否有厨师权限
$current_user = current_user();
if (!$current_user || ($current_user['role'] != 'chef' && $current_user['role'] != 'admin')) {
    header('HTTP/1.1 403 Forbidden');
    echo json_encode(['success' => false, 'message' => '无权限访问']);
    exit;
}

// 获取待处理和制作中的订单
$orders = Database::fetchAll("SELECT o.*, u.name as member_name 
                             FROM orders o 
                             LEFT JOIN users u ON o.user_id = u.id 
                             WHERE o.status IN ('paid', 'cooking') 
                             ORDER BY o.created_at ASC");

// 获取每个订单的详细商品
foreach ($orders as $key => $order) {
    $items = Database::fetchAll("SELECT * FROM order_items WHERE order_id = ?", [$order['id']]);
    $orders[$key]['items'] = $items;
}

// 返回JSON数据
header('Content-Type: application/json');
echo json_encode([
    'success' => true,
    'orders' => $orders
]); 